﻿using System;
using System.ServiceModel;

namespace WCFPeerResolver
{
    /// <summary>
    /// Mesh entry represents a client in the mesh.
    /// </summary>
    public class MeshEntry
    {
        /// <summary>
        /// ID of the client.
        /// </summary>
        public readonly Guid ClientID;

        /// <summary>
        /// ID of the entry registration.
        /// </summary>
        public readonly Guid RegistrationID;

        /// <summary>
        /// Address information about the peer.
        /// </summary>
        public readonly PeerNodeAddress Address;

        /// <summary>
        /// Timestamp of when the mesh entry was last updated.
        /// </summary>
        public DateTime Updated { get; private set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="clientID">ID of the client to register.</param>
        /// <param name="address">Address information of the client.</param>
        public MeshEntry(Guid clientID, PeerNodeAddress address)
        {
            RegistrationID = Guid.NewGuid();
            ClientID = clientID;
            Address = address;
            Updated = DateTime.UtcNow;
        }

        /// <summary>
        /// Updates the update propperty used by the cleanup method 
        /// to determine if the entry is to be removed.
        /// </summary>
        /// <remarks>For internal use only.</remarks>
        /// <param name="time">Time to update the Updated property with.</param>
        internal void Refresh(DateTime time)
        {
            Updated = time;
        }

        /// <summary>
        /// Shallow Clone the entry.
        /// </summary>
        /// <remarks>
        /// Since the entry is recreated on update there is no need to deep clone the object.
        /// </remarks>
        /// <returns>The new shallow cloned entry.</returns>
        internal MeshEntry Clone()
        {
            MeshEntry entry = new MeshEntry(ClientID, Address);
            return entry;
        }
    }
}